'use strict';

angular.module('ProduceWork').controller('MOOnlineController', ['$scope', '$resource', 'EntityService', 'PublicModule', 'PublicEnum', 'PublicFunction', 'BarcodeEnum', '$location', 'usbPort', 'Authentication',
    function($scope, $resource, EntityService, PublicModule, PublicEnum, PublicFunction, BarcodeEnum, $location, usbPort, Authentication) {
        PublicModule.initModule($scope, 'MO');
        $scope.Module.MO.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
        $scope.Module.MO.Condition = {$where : "this.OnlineQty < this.Qty"};
        $scope.Module.MO.Populate = [{path: 'ItemMaster', select: 'Code Name'}];
        $scope.Module.MO.PageSize = 15;

        PublicModule.initModule($scope, 'BarcodeMaster');
        $scope.Module.BarcodeMaster.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BarcodeMaster.PageSize = 50;
        $scope.Module.BarcodeMaster.OrderBy = {OrderNum: 1};

        $scope.btnOnLine = function(){
            $scope.Module.MO.FocusedRecord.currentQty = 0;
            $scope.Module.BarcodeMaster.Records = [];
            usbPort.start();
        };
        $scope.onModalHide = function () {
            usbPort.stop();
        };
        $scope.$on('usbPort.input', function (e, inputStr) {
            $scope.addChipRow(inputStr);
        });

        $scope.addChipRow = function(hexID){
            if ($scope.Module.MO.FocusedRecord.Qty - $scope.Module.MO.FocusedRecord.OnlineQty <= 0) {
                return;
            };
            var maxLineValue = 0;
            for(var i=0; i<$scope.Module.BarcodeMaster.Records.length; i++){
                if($scope.Module.BarcodeMaster.Records[i].Barcode == hexID){
                    return;
                }

                if($scope.Module.BarcodeMaster.Records[i].OrderNum !== "" && 
                    $scope.Module.BarcodeMaster.Records[i].OrderNum != undefined && 
                    $scope.Module.BarcodeMaster.Records[i].OrderNum > maxLineValue)
                {
                    maxLineValue = $scope.Module.BarcodeMaster.Records[i].OrderNum;
                }
            }
            var newRecord = $scope.Module.BarcodeMaster.add();
            newRecord.OrderNum = maxLineValue + 1;
            newRecord.Barcode = hexID;
            newRecord.BarcodeType = BarcodeEnum.BarcodeType.ItemMaster.Value;
            newRecord.BusinessStatus = BarcodeEnum.BusinessStatus.Start.Value;
            newRecord.MO = $scope.Module.MO.FocusedRecord._id;
            newRecord.MOCode = $scope.Module.MO.FocusedRecord.Code;
            newRecord.ItemMaster = $scope.Module.MO.FocusedRecord.ItemMaster._id;
            newRecord.ItemMasterCode = $scope.Module.MO.FocusedRecord.ItemMaster.Code;
            newRecord.ItemMasterName = $scope.Module.MO.FocusedRecord.ItemMaster.Name;
            newRecord.MOLotNumber = $scope.Module.MO.FocusedRecord.LotNumber;
            
            //持久化
            var saveDatas = [];
            saveDatas.push({
                EntityName: 'BarcodeMaster',
                Records: [newRecord]
            });
            $scope.Module.BarcodeMaster.emitOnLoadEvent();
            
            if($scope.Module.MO.FocusedRecord.OnlineQty == null || $scope.Module.MO.FocusedRecord.OnlineQty == undefined){
                $scope.Module.MO.FocusedRecord.OnlineQty = 1;
                $scope.Module.MO.FocusedRecord.StartDateTime = new Date();
            }
            else{
                $scope.Module.MO.FocusedRecord.OnlineQty += 1;
            }
            $scope.Module.MO.FocusedRecord.RowStatus = PublicEnum.RowStatus.Modify;
            saveDatas.push({
                EntityName: 'MO',
                Records: [$scope.Module.MO.FocusedRecord]
            });
            EntityService.saves(saveDatas, function(res){
                //刷新MO数据
                $scope.Module.MO.findById($scope.Module.MO.FocusedRecord._id, function(record){
                    $scope.Module.MO.FocusedRecord = record;
                    $scope.Module.MO.emitRowsDataChangeEvent([record]);
                });

                var req = {
                    Parameter: {
                        Barcode: hexID
                    }
                };
                $resource('/api/Kanban/createKanban').save(
                    req,
                    function (res) {
                        //打印
                        $scope.printBarcode();

                        PublicFunction.tip('上线成功', '条码编号：' + newRecord.Barcode);
                    },
                    function (err) {
                        PublicFunction.alert(err.message, err.title);
                    }
                );
            });
        };

        $scope.printBarcode = function(){
            if($scope.Module.BarcodeMaster.FocusedRecord != null){
                PublicFunction.alert($scope.Module.BarcodeMaster.FocusedRecord.Barcode, '打印条码');
            }
            angular.element(".modal-body").printThis();
        };

        // $scope.btnDelete = function(){
        //  $scope.Module.MO.deleteSelecteds(function(){
        //      $scope.Module.MO.load();
        //  });
        // };
    }
]);
